﻿using MediatR;
using Microsoft.EntityFrameworkCore.Diagnostics;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.Reports
{
    public class Check_InDetailQueryHandler : IRequestHandler<Check_InDetailQueryCommand, Result>
    {
        /// <summary>
        /// 进场登记
        /// </summary>
        private readonly IBaseRepositoryGet<CheckIn> checkInRepository;
        /// <summary>
        /// 结算类型
        /// </summary>
        private readonly IBaseRepositoryGet<SettlementType> settlementTypeRepository;
        /// <summary>
        /// 供应商
        /// </summary>
        private readonly IBaseRepositoryGet<Supplier> supplierRepository;
        /// <summary>
        /// 活畜称重
        /// </summary>
        private readonly IBaseRepositoryGet<LiveAnimals> liveAnimalsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="checkInRepository"></param>
        /// <param name="settlementTypeRepository"></param>
        /// <param name="supplierRepository"></param>
        public Check_InDetailQueryHandler(IBaseRepositoryGet<CheckIn> checkInRepository, IBaseRepositoryGet<SettlementType> settlementTypeRepository, IBaseRepositoryGet<Supplier> supplierRepository, IBaseRepositoryGet<LiveAnimals> liveAnimalsRepository)
        {
            this.checkInRepository = checkInRepository;
            this.settlementTypeRepository = settlementTypeRepository;
            this.supplierRepository = supplierRepository;
            this.liveAnimalsRepository = liveAnimalsRepository;
        }

        public async Task<Result> Handle(Check_InDetailQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Success };
            try
            {
                var list =(from a in checkInRepository.GetAll()
                            join b in settlementTypeRepository.GetAll()
                            on a.SettlementTypeId equals b.SettlementTypeId
                            join c in supplierRepository.GetAll()
                            on a.SupplierId equals c.SupplierId
                            join d in liveAnimalsRepository.GetAll()
                            on a.CheckInId equals d.CheckInId
                            where a.CheckInIsDel==false
                            select new
                            {
                                d.LiveAnimalsDate,
                                a.CheckInId,
                                a.CheckInNo,
                                a.CarNo,
                                a.ApproachNum,
                                b.SettlementTypeId,
                                b.SettlementTypeName,
                                c.SupplierId,
                                c.SupplierName,
                                a.QuarantineNo,
                                a.OriginAddr,
                                a.CheckInBatch,
                                a.Deliveryman,
                                a.Phone,
                                a.IdentityCard,
                                a.Remark,
                                a.EnterRegistrar,
                                a.GoOutRegistrar,
                                a.QuarantineImg,
                                a.CheckInStateId,
                                a.EnterTime,
                                a.GoOutTime
                            }).ToList();

                if (request.SupplierId != 0)
                {
                    list = list.Where(x => x.SupplierId == request.SupplierId).ToList();
                }
                if (request.SettlementTypeId != 0)
                {
                    list = list.Where(x => x.SettlementTypeId == request.SettlementTypeId).ToList();
                }
                if (!string.IsNullOrEmpty(request.QuarantineNo))
                {
                    list = list.Where(x => x.QuarantineNo.Contains(request.QuarantineNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.OriginAddr))
                {
                    list = list.Where(x => x.OriginAddr.Contains(request.OriginAddr)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CheckInNo))
                {
                    list = list.Where(x => x.CheckInNo.Contains(request.CheckInNo)).ToList();
                }
                // 进场登记时间
                if (request.EnterTime != null)
                {
                    list = list.Where(x => x.EnterTime >= request.EnterTime).ToList();
                }

                var totalCount = list.Count;
                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                list = list.OrderBy(x => x.CheckInId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                //返回元组
                var CheckInDetail = new
                {
                    totalCount,
                    totalPage,
                    list
                };

                //如果有数据就返回
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = CheckInDetail;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }

            return result;
           
        }
    }
}
